﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class hydevmanager_admin_add_news : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["timeout"] == null)
            {
                Response.Redirect("login.aspx");
            }
            else
            {
                //Load_DataName();
                txt_tentour.Focus();
                Session.Add("fileName", "");
                string str = Session["fileName"].ToString();

                string idnews = Request.QueryString.Get("idnew");
                if (idnews == null)
                {
                    Response.Redirect("admin_add_vemaybay.aspx?idnew=3");

                }
                else
                {
                    if (idnews == "3")
                    {
                        drMenu.Enabled = true;
                        btninsert.Enabled = true;
                        btnupdate.Enabled = false;
                        load_drop2();
                        load_drop3();
                        load_drop_diadanh();
                    }
                    else
                    {

                        btninsert.Enabled = false;
                        btnupdate.Enabled = true;
                        load_news_id(idnews);
                        Session.Add("idnew1", idnews);
                    }//?menu=14&parent=6
                    if (idnews != null && Request.QueryString.Get("menu") != null && Request.QueryString.Get("parent") != null)
                    {
                        string linkweb = "?menu=" + Request.QueryString.Get("menu").ToString() + "&parent=" +
                                         Request.QueryString.Get("parent");
                        filldrop(linkweb);
                        if (idnews != "3")
                        {
                            filldrop_diemden_vemaybay(idnews);
                            filldrop_diemden(idnews);
                            filldrop_xuatphat(idnews);
                        }

                    }
                }
            }
        }
    }
    protected void filldrop(string link_web)
    {
        DataTable dtdrmenu = new DataTable();
        dtdrmenu = DataAccess.ExeSQLToDataTable("select * from dev_menu where link_web='" + link_web + "'");
        int n = dtdrmenu.Rows.Count;
        if (n > 0)
        {
            drMenu.DataSource = dtdrmenu;
            drMenu.DataTextField = "name_menu";
            drMenu.DataValueField = "link_web";
            drMenu.DataBind();

            for (int i = 0; i < n; i++)
            {
                if (dtdrmenu.Rows[i][3].ToString() == link_web)
                {
                    drMenu.Items[i].Selected = true;
                }
            }
        }
    }
    protected void filldrop_diemden(string idnew)
    {
        DataTable dtdrmenu = new DataTable();
        dtdrmenu = DataAccess.ExeSQLToDataTable("select * from dev_news where idnews='" + idnew + "'");
        DataTable dtdiemden = DataAccess.ExeSQLToDataTable("select * from diadanh");
        int n = dtdrmenu.Rows.Count;
        if (n > 0)
        {
            drdiemden.DataSource = dtdiemden;
            drdiemden.DataTextField = "Name";
            drdiemden.DataValueField = "IdYahoo";
            drdiemden.DataBind();

            for (int i = 0; i < dtdiemden.Rows.Count; i++)
            {
                if (dtdiemden.Rows[i]["IdYahoo"].ToString() == dtdrmenu.Rows[0]["diemden"].ToString())
                {
                    drdiemden.Items[i].Selected = true;
                }
            }
        }
    }
    protected void filldrop_xuatphat(string idnew)
    {
        DataTable dtdrmenu = new DataTable();
        dtdrmenu = DataAccess.ExeSQLToDataTable("select * from dev_news where idnews='" + idnew + "'");
        DataTable dtdiemden = DataAccess.ExeSQLToDataTable("select * from diadanh");
        int n = dtdrmenu.Rows.Count;
        if (n > 0)
        {
            drxuatphat.DataSource = dtdiemden;
            drxuatphat.DataTextField = "Name";
            drxuatphat.DataValueField = "IdYahoo";
            drxuatphat.DataBind();

            for (int i = 0; i < dtdiemden.Rows.Count; i++)
            {
                if (dtdiemden.Rows[i]["IdYahoo"].ToString() == dtdrmenu.Rows[0]["diemxuatphat"].ToString())
                {
                    drxuatphat.Items[i].Selected = true;
                }
            }
        }
    }
    protected void filldrop_diemden_vemaybay(string idnew)
    {
        DataTable dtdrmenu = new DataTable();
        dtdrmenu = DataAccess.ExeSQLToDataTable("select * from dev_news where idnews='" + idnew + "'");
        DataTable dtdiemden = DataAccess.ExeSQLToDataTable("select * from HangBay");
        int n = dtdrmenu.Rows.Count;
        if (n > 0)
        {
            drhangbay.DataSource = dtdiemden;
            drhangbay.DataTextField = "Name";
            drhangbay.DataValueField = "Id";
            drhangbay.DataBind();

            for (int i = 0; i < dtdiemden.Rows.Count; i++)
            {
                if (dtdiemden.Rows[i]["Id"].ToString() == dtdrmenu.Rows[0]["HangBay"].ToString())
                {
                    drhangbay.Items[i].Selected = true;
                }
            }
        }
    }
    protected string load_anh()
    {
        string html = "";
        if (Session["idnew1"] == null)
        {
            html += "<img alt=''  class='imgcheck' runnat='server' src='../files_upload/images/vemaybay/default.jpg'></img>";
        }
        else
        {
            string idnews = Session["idnew1"].ToString();
            DataTable dtcheckanh = new DataTable();
            dtcheckanh = DataAccess.ExeSQLToDataTable("select * from dev_news where idnews='" + idnews + "'");
            if (dtcheckanh.Rows.Count > 0)
            {
                if (dtcheckanh.Rows[0][7].ToString() != "")
                {
                    html += "<img alt=''  class='imgcheck' runnat='server' src='../files_upload/images/vemaybay/" + dtcheckanh.Rows[0][7].ToString() + "'></img>";
                    // Image kt=System.Drawing.Image=System.Drawing.Image.FromFile(Server.MapPath(dtcheckanh.Rows[0][7].ToString()));
                    try
                    {
                        string PicturePath = Server.MapPath("../files_upload/images/vemaybay/" + dtcheckanh.Rows[0][7].ToString());
                        System.Drawing.Image image = System.Drawing.Image.FromFile(PicturePath);
                        int ActualWidth = image.Width;
                        int ActualHeight = image.Height;
                        html += "<div class='height_width'>";
                        html += "Chiều rộng:" + " " + ActualWidth + " " + "px";
                        html += "--";
                        html += "Chiều cao: " + " " + ActualHeight + " " + "px";
                        html += "</div>";
                    }
                    catch
                    {


                    }


                }
                else
                {



                }
            }

        }
        return html;
    }

    protected void Dr_select_Change(object sender, EventArgs e)
    {
        string idmenu = drMenu.SelectedItem.Value.ToString();
        if (idmenu == "?menu=168&parent=163")
        {
            idmenu = "?menu=175&parent=173";
        }
        else
        {
            if (idmenu == "?menu=169&parent=163")
            {
                idmenu = "?menu=174&parent=173";
            }
        }
        DataTable dt = DataAccess.ExeSQLToDataTable("select * from DiaDanh where CheckTour='" + idmenu + "'");
        drdiemden.DataValueField = "IdYahoo";
        drdiemden.DataTextField = "Name";
        drdiemden.DataSource = dt;
        drdiemden.DataBind();


    }
    protected void luu_click(object sender, EventArgs e)
    {
        //try
        //{


            string title = txt_tentour.Text;
            string diemxuatphat = "";
            if (drdiemden.SelectedItem.Value != null)
            {
                diemxuatphat = drxuatphat.SelectedValue;
            }

            string diemden = drdiemden.SelectedValue.ToString();
            string hangbay = drhangbay.SelectedItem.Value.ToString();
            bool vemaybay = true;
            string hotline = txt_hotline.Text;
            string link_web = drMenu.SelectedItem.Value;
            string body = fcknew.Value.Trim().Replace("'","");
            string filename = fileupload1.FileName;
            string khuhoi = txtkhuhoi.Value;
            // string username = Session["Name"].ToString();

            DateTime createtime = DateTime.Now;
            decimal? tietkiem = null;


            decimal? giatour = null;
            giatour = txt_giatour.Text == null ? 0 : Convert.ToDecimal(txt_giatour.Text);
            //try
            //{
            if (filename != "")
            {
                fileupload1.SaveAs(Server.MapPath("~/files_upload/images/vemaybay/" + filename));
                //DataAccess.ExeNonStored("Dev_news_Insert", "@title", title, "@link_web", link_web, "@sapo", sapo,
                //                        "@body", body, "@picture", filename, "@createtime", createtime, "@createby", username);
                DataAccess.ExeNonSQL("insert into dev_news(title,body,link_web,diemxuatphat,diemden,giatour,hotline,picture,vemaybay,HangBay,khuhoi) values(N'" + title + "',N'" + body + "','" + link_web + "',N'" + diemxuatphat + "',N'" + diemden + "','" + giatour + "',N'" + hotline + "',N'" + filename + "','" + vemaybay + "',N'" + hangbay + "',N'" + khuhoi + "')");

                Response.Redirect("admin_manager_vemaybay.aspx");
            }
            else
            {
                DataAccess.ExeNonSQL("insert into dev_news(title,body,link_web,diemxuatphat,diemden,giatour,hotline,vemaybay,HangBay) values(N'" + title + "',N'" + body + "','" + link_web + "',N'" + diemxuatphat + "',N'" + diemden + "','" + giatour + "',N'" + hotline + "','" + vemaybay + "',N'" + hangbay + "',N'" + hangbay + "')");
                Response.Redirect("admin_manager_vemaybay.aspx");
            }

        //}
        //catch (Exception ex)
        //{


        //}

    }
    protected void update_click(object sender, EventArgs e)
    {

        string title = txt_tentour.Text;
        string diemxuatphat = "";
        if (drdiemden.SelectedItem.Value != null)
        {
            diemxuatphat = drxuatphat.SelectedValue;
        }

        string diemden = drdiemden.SelectedValue.ToString();
        string khuhoi = txtkhuhoi.Value;
        bool vemaybay = true;
        string hotline = txt_hotline.Text;
        string link_web = drMenu.SelectedItem.Value;
        string body = fcknew.Value.Trim().Replace("'", "");
        string filename = fileupload1.FileName;
        string hangbay = drhangbay.SelectedItem.Value.ToString();
        // string username = Session["Name"].ToString();

        DateTime createtime = DateTime.Now;
        decimal? tietkiem = null;
        decimal? giatour = null;
        giatour = txt_giatour.Text == null ? 0 : Convert.ToDecimal(txt_giatour.Text);
        if (Session["idnew1"] == null)
        {
            Response.Redirect("admin_manager_vemaybay.aspx");
        }
        else
        {
            if (Session["Name"] == null)
            {
                Response.Redirect("login.aspx");
            }
            else
            {
                DateTime uptime = DateTime.Now;
                string idnews = Session["idnew1"].ToString();
                if (filename != "")
                {
                    fileupload1.SaveAs(Server.MapPath("~/files_upload/images/vemaybay/" + filename));
                    DataAccess.ExeNonSQL("update dev_news set title=N'" + title + "',body=N'" + body + "',diemxuatphat=N'" + diemxuatphat + "',link_web='" + link_web + "',diemden=N'" + diemden + "',giatour='" + giatour + "',hotline=N'" + hotline + "',picture=N'" + filename + "',HangBay=N'" + hangbay + "',khuhoi=N'" + khuhoi + "' where idnews='" + idnews + "'");
                    //DataAccess.ExeNonStored("Dev_news_update", "@idnews", idnews, "@title", title, "@sapo", sapo, "@body", body, "@picture", filename, "@updatetime", uptime, "@updateby", username, "@linkweb", link_web);
                    Response.Redirect("admin_manager_vemaybay.aspx");
                }
                else
                {
                    //DataAccess.ExeNonSQL("update dev_news set title=N'" + title + "',sapo=N'" + sapo + "',body=N'" + body + "',updatetime='" + uptime + "',updateby='" + username + "' where idnews='" + idnews + "'");
                    //DataAccess.ExeNonStored("Dev_news_update_noimg", "@idnews", idnews, "@title", title, "@sapo", sapo, "@body", body, "@updatetime", uptime, "@updateby", username, "@linkweb", link_web);
                    DataAccess.ExeNonSQL("update dev_news set title=N'" + title + "',body=N'" + body + "',diemxuatphat=N'" + diemxuatphat + "',link_web='" + link_web + "',diemden=N'" + diemden + "',giatour='" + giatour + "',hotline=N'" + hotline + "',HangBay=N'" + hangbay + "',khuhoi=N'" + khuhoi + "' where idnews='" + idnews + "'");
                    Response.Redirect("admin_manager_vemaybay.aspx");
                }
            }
        }

    }
    protected void update_click_quay(object sender, EventArgs e)
    {
        Response.Redirect("admin_manager_vemaybay.aspx");
    }
    protected void load_news_id(string id)
    {
        string sql_load_new = "select * from dev_news where idnews='" + id + "' ";
        DataTable dtnewup = new DataTable();
        dtnewup = DataAccess.ExeSQLToDataTable(sql_load_new);
        if (dtnewup.Rows.Count > 0)
        {
            txt_tentour.Text = dtnewup.Rows[0]["title"].ToString();
            //txt_diemxuatphat.Text = dtnewup.Rows[0]["diemxuatphat"].ToString();
            //txt_diemden.Text = dtnewup.Rows[0]["diemden"].ToString();
            txt_giatour.Text = dtnewup.Rows[0]["giatour"].ToString();
            txt_hotline.Text = dtnewup.Rows[0]["hotline"].ToString();
            fcknew.Value = dtnewup.Rows[0]["body"].ToString();
            txtkhuhoi.Value = dtnewup.Rows[0]["khuhoi"].ToString();
        }
    }

    #region Load Drop theo đệ quy
    protected DataTable load_drop_process(DataTable dtDrop, DataTable dtnews, string IdParent, int level)
    {
        int countall = dtnews.Rows.Count;

        for (int i = 0; i < countall; i++)
        {
            if (dtnews.Rows[i][1].ToString() == IdParent)
            {
                string idMenu = dtnews.Rows[i][0].ToString();

                //Khai 1 bao doi tuong DataRow
                DataRow drItems;
                drItems = dtDrop.NewRow();

                drItems["name_menu"] = levelMenu(level) + dtnews.Rows[i][2].ToString();
                drItems["link_web"] = dtnews.Rows[i][3].ToString();
                dtDrop.Rows.Add(drItems);

                load_drop_process(dtDrop, dtnews, idMenu, level + 1);
            }
        }
        return dtDrop;
    }

    protected string levelMenu(int value)
    {
        string str = "";
        for (int i = 0; i < value; i++)
        {
            str += "*";
        }
        return str;
    }

    protected void load_drop2()
    {
        DataTable dtDrop = new DataTable();
        dtDrop.Columns.Add("name_menu", System.Type.GetType("System.String"));
        dtDrop.Columns.Add("link_web", System.Type.GetType("System.String"));

        DataTable dtDatadrop = DataAccess.ExeSQLToDataTable("SELECT * FROM dev_Menu");

        load_drop_process(dtDrop, dtDatadrop, "0", 0);

        drMenu.DataTextField = "name_menu";
        drMenu.DataValueField = "link_web";
        drMenu.DataSource = dtDrop;
        drMenu.DataBind();
    }
    protected void load_drop3()
    {
        DataTable dtDatadrop = DataAccess.ExeSQLToDataTable("SELECT * FROM HangBay ");
        drhangbay.DataTextField = "Name";
        drhangbay.DataValueField = "Id";
        drhangbay.DataSource = dtDatadrop;
        drhangbay.DataBind();
        drhangbay.Items.Insert(0, "---Xin chọn hãng máy báy--- ");
        drhangbay.SelectedIndex = 1;
    }
    protected void load_drop_diadanh()
    {
        DataTable dtDatadrop = DataAccess.ExeSQLToDataTable("SELECT * FROM DiaDanh ");
        drdiemden.DataTextField = "Name";
        drdiemden.DataValueField = "IdYahoo";
        drdiemden.DataSource = dtDatadrop;
        drdiemden.DataBind();
        drxuatphat.DataTextField = "Name";
        drxuatphat.DataValueField = "IdYahoo";
        drxuatphat.DataSource = dtDatadrop;
        drxuatphat.DataBind();
    }
    #endregion
}